﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace lcs.BLL
{
    public class Tools
    {
        /// <summary>
        /// 将框架页面添加到Application中
        /// </summary>
        /// <param name="d">所有权限列表</param>
        /// <returns></returns>
        public static Dictionary<string, int> AddApplication(Dictionary<string, int> d)
        {
            d.Add("center.html", 100000);
            d.Add("down.html", 200000);
            d.Add("middel.html", 300000);
            d.Add("right.html", 400000);
            d.Add("right_top.html", 50000);
            d.Add("NoRights.aspx", 600000);
            d.Add("Error.aspx", 700000);
            d.Add("Welcome.aspx", 800000);
            d.Add("main.aspx", 900001);
            d.Add("top.aspx", 900002);
            d.Add("left.aspx", 900003);

            return d;
        }

        /// <summary>
        /// 将框架页面权限添加到用户权限列表中
        /// </summary>
        /// <param name="l">用户权限列表</param>
        /// <returns></returns>
        public static List<int> AddSession(List<int> l)
        {
            l.Add(100000);
            l.Add(200000);
            l.Add(300000);
            l.Add(400000);
            l.Add(500000);
            l.Add(600000);
            l.Add(700000);
            l.Add(800000);
            l.Add(900001);
            l.Add(900002);
            l.Add(900003);

            return l;
        }

        public static List<int> GetNextFlowUserId(int person_id,int dpid)
        {
            List<int> result = new List<int>();
            Maticsoft.BLL.workFlow wm = new Maticsoft.BLL.workFlow();

            List<Maticsoft.Model.documentFlowInfo> li = new Maticsoft.BLL.documentFlowInfo().GetModelList(" document_id = " + dpid);

            if (li.Count == 1)
            {
                Maticsoft.Model.documentFlowInfo d = li[0];

                //获取当前审批人审批节点号，transfer的undefine4记录的是该由哪个审批人审批的节点号，如果要修改自身的workFlow，则节点号就是undefine4
                int flow_node_num_now = Convert.ToInt32(d.flow_node_num_now);
                int flow_node_id_now = 0;

                flow_node_id_now = wm.GetFlow_node_id_now(person_id, d.document_id.Value, flow_node_num_now);

                int flow_node_id = wm.GetFlow_node_id(person_id, d.document_id.Value, Convert.ToInt32(d.flow_node_num_now),d.flow_id.Value);

                string str = wm.Submit_judge(flow_node_id, person_id, d.document_id.Value, Convert.ToInt32(d.flow_node_num_now));//得到审核状态

                if (str == "同意")//1、如果是会签非尾节点同意，插入下一节点
                {
                   
                }

                if (str == "尾节点同意")//2、如果是会签尾节点同意
                {
                    
                }

                if (str == "非会签")//3、如果是非会签非尾节点，插入下一结点
                {
                    int flow_node_num = d.flow_node_num_now.Value + 1;
                    DataSet ds = wm.GetFlowNodeByFlow_idAndFlow_node_num(d.flow_id.Value, flow_node_num);

                    int re = Convert.ToInt32(ds.Tables[0].Rows[0]["person_id"].ToString());

                    result.Add(re);

                }

                if (str == "尾节点非会签")
                {
                    Maticsoft.BLL.documentFlowMapping dfmapm = new Maticsoft.BLL.documentFlowMapping();
                    //获取流程图
                    List<Maticsoft.Model.documentFlowMapping> li_map = dfmapm.GetModelList
                        (" documentFlowMapping_name='" + d.documentFlowMapping_name + "'  and flow_id= " + d.flow_id);
                    if (li_map.Count == 1)
                    {
                        Maticsoft.Model.documentFlowMapping dmap = li_map[0];

                        //获取下一个流程信息
                        Maticsoft.Model.documentFlowMapping dmap_next = dfmapm.GetModelList
                            (" documentFlowMapping_name='" + d.documentFlowMapping_name +
                            "' and flow_order = " + (dmap.flow_order.Value + 1) + " ")[0];

                        Maticsoft.BLL.flow_node fm = new Maticsoft.BLL.flow_node();
                        Maticsoft.Model.flow_node f = fm.GetModelList(" flow_id=" + dmap_next.flow_id + " and flow_node_num = 1 ")[0];

                        DataSet ds_workFlow = wm.GetWorkFlow(dmap_next.flow_id.Value, 1);

                        int re = Convert.ToInt32(ds_workFlow.Tables[0].Rows[0]["person_id"].ToString());

                        result.Add(re);
                    }


                }

            }

            return result;

        }

        //非尾节点的处理
        public static void DealWorkFlow(Maticsoft.Model.documentFlowInfo t, int flow_node_id, int submitId)
        {
            Maticsoft.BLL.flow_node fm = new Maticsoft.BLL.flow_node();
            Maticsoft.Model.flow_node f = fm.GetModel(flow_node_id);//获取flow_node

            int flow_node_num = Convert.ToInt32(f.flow_node_num) + 1;//获取下一节点的号码

            t.status = 1;//公文审批中
            t.flow_node_num_now = flow_node_num;//设置公文流程步骤+1，即下一节点的号码

            try
            {
                new Maticsoft.BLL.documentFlowInfo().Update(t);//修改公文为处理中
            }
            catch (Exception ex)
            {

                throw ex;
            }

            //获取下一结点的所有信息
            Maticsoft.BLL.workFlow wmm = new Maticsoft.BLL.workFlow();
            DataSet ds = wmm.GetFlowNodeByFlow_idAndFlow_node_num(t.flow_id.Value, flow_node_num);
            Maticsoft.Model.flow_node f_next = fm.GetModelList(" flow_id=" + t.flow_id + " and flow_node_num = " + flow_node_num)[0];

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Maticsoft.Model.workFlow w = new Maticsoft.Model.workFlow();
                w.document_id = t.document_id;
                w.flow_node_id = Convert.ToInt32(ds.Tables[0].Rows[i]["flow_node_id"].ToString());
                w.flow_node_num = Convert.ToInt32(ds.Tables[0].Rows[i]["flow_node_num"].ToString());
                w.person_id = Convert.ToInt32(ds.Tables[0].Rows[i]["person_id"].ToString());
                w.workFlow_direction = 0;//代表此节点是正向，非反向（退签）
                w.workFlow_reson = "";
                w.workFlow_status = 0;//审批状态(0、未审批，1、同意，2、否决(退回首节点))
                w.workFlow_time = DateTime.Now;
                w.workFlow_isWork = f_next.flow_node_isAutoNext;//根据流程节点是否自动分发设置此流程节点是否处于激活状态
                w.workFlow_submitId = submitId;

                wmm.Add(w);
            }


        }

        //尾节点处理
        public static void DealLast(Maticsoft.Model.documentFlowInfo t, int submitId)
        {
            //判断是否是最后一个流程，如果不是则插入下一流程id，如果是，则流程完毕
            Maticsoft.BLL.documentFlowInfo dfm = new Maticsoft.BLL.documentFlowInfo();
            Maticsoft.BLL.documentFlowMapping dfmapm = new Maticsoft.BLL.documentFlowMapping();
            //获取流程图
            List<Maticsoft.Model.documentFlowMapping> li_map = dfmapm.GetModelList
                (" documentFlowMapping_name='" + t.documentFlowMapping_name + "'  and flow_id= " + t.flow_id);

            if (li_map.Count == 1)
            {
                Maticsoft.Model.documentFlowMapping dmap = li_map[0];

                t.isFlowDone = true;//此流程结束


                if (dmap.isLastFlow)//判断是否为最后一个流程
                {
                    t.isDone = true;//更改状态为处理完结
                }
                else
                {

                    //获取下一个流程信息
                    Maticsoft.Model.documentFlowMapping dmap_next = dfmapm.GetModelList
                        (" documentFlowMapping_name='" + t.documentFlowMapping_name +
                        "' and flow_order = " + (dmap.flow_order.Value + 1) + " ")[0];

                    t.flow_id = dmap_next.flow_id;//设置下一个流程ID
                    t.flow_name = dmap_next.flow_name;//设置下一个流程名称

                    Maticsoft.BLL.flow_node fm = new Maticsoft.BLL.flow_node();
                    Maticsoft.Model.flow_node f = fm.GetModelList(" flow_id=" + t.flow_id + " and flow_node_num = 1 ")[0];

                    t.flow_node_id_now = f.flow_node_id;//设置当前节点
                    t.flow_node_num_now = f.flow_node_num;//当前节点顺序号
                    t.status = 1;//审批中
                    t.isDone = false;//设置总流程没有完结

                    //加入下一节点审核信息
                    Maticsoft.BLL.workFlow wmm = new Maticsoft.BLL.workFlow();
                    DataSet ds_workFlow = wmm.GetWorkFlow(t.flow_id.Value, t.flow_node_num_now.Value);
                    for (int i = 0; i < ds_workFlow.Tables[0].Rows.Count; i++)
                    {
                        Maticsoft.Model.workFlow w = new Maticsoft.Model.workFlow();
                        w.document_id = t.document_id;
                        w.flow_node_id = Convert.ToInt32(ds_workFlow.Tables[0].Rows[i]["flow_node_id"].ToString());
                        w.flow_node_num = Convert.ToInt32(ds_workFlow.Tables[0].Rows[i]["flow_node_num"].ToString());
                        w.person_id = Convert.ToInt32(ds_workFlow.Tables[0].Rows[i]["person_id"].ToString());
                        w.workFlow_direction = 0;//代表此节点是正向，非反向（退签）
                        w.workFlow_reson = "";
                        w.workFlow_status = 0;//审批状态(0、未审批，1、同意，2、否决(退回首节点))
                        w.workFlow_time = DateTime.Now;
                        w.workFlow_isWork = f.flow_node_isAutoNext;//根据流程节点是否自动分发设置此流程节点是否处于激活状态
                        w.workFlow_submitId = submitId;

                        wmm.Add(w);
                    }

                }

                dfm.Update(t);

            }

        }

        //同意处理
        public static void DealAgree(int dpid,int person_id,string reason)
        {
            Maticsoft.BLL.documentFlowInfo dm = new Maticsoft.BLL.documentFlowInfo();
            Maticsoft.BLL.workFlow wm = new Maticsoft.BLL.workFlow ();
            List<Maticsoft.Model.documentFlowInfo> li = new Maticsoft.BLL.documentFlowInfo().GetModelList(" document_id = " + dpid);

            if (li.Count == 1)
            {
                Maticsoft.Model.documentFlowInfo d = li[0];

                //获取当前审批人审批节点号
                int flow_node_num_now = Convert.ToInt32(d.flow_node_num_now);
                int flow_node_id_now = 0;

                flow_node_id_now = wm.GetFlow_node_id_now(person_id, d.document_id.Value, flow_node_num_now);

                DataSet ds_nowWorkFlowInfo = wm.GetNowWorkFlowInfo(d.document_id.Value.ToString(), flow_node_id_now.ToString(), flow_node_num_now);

                DataRow r_now = ds_nowWorkFlowInfo.Tables[0].Rows[0];

                if (wm.Update(1, reason, DateTime.Now, r_now["workFlow_id"].ToString()))//修改本身意见为同意,添加处理意见、处理时间
                {
                    int flow_node_id = wm.GetFlow_node_id(person_id, d.document_id.Value, Convert.ToInt32(d.flow_node_num_now),d.flow_id.Value);

                    string str = wm.Submit_judge(flow_node_id, person_id, d.document_id.Value, Convert.ToInt32(d.flow_node_num_now));//得到审核状态

                    if (str == "同意")//1、如果是会签非尾节点同意，插入下一节点
                    {
                        lcs.BLL.Tools.DealWorkFlow(d, flow_node_id, person_id);

                    }

                    if (str == "尾节点同意")//2、如果是会签尾节点同意
                    {
                        lcs.BLL.Tools.DealLast(d, person_id);

                    }

                    if (str == "非会签")//3、如果是非会签非尾节点，插入下一结点
                    {
                        lcs.BLL.Tools.DealWorkFlow(d, flow_node_id, person_id);
                    }

                    if (str == "尾节点非会签")
                    {
                        lcs.BLL.Tools.DealLast(d, person_id);
                    }
                }
            }

               
        }

        /// <summary>
        /// 否决处理返回上一步
        /// </summary>
        /// <param name="dpid"></param>
        /// <param name="person_id"></param>
        /// <param name="reason"></param>
        public static void DealDisagreeLast(int dpid, int person_id,string reason)
        { 
            Maticsoft.BLL.documentFlowInfo dm = new Maticsoft.BLL.documentFlowInfo();
            Maticsoft.BLL.workFlow wm = new Maticsoft.BLL.workFlow ();
            List<Maticsoft.Model.documentFlowInfo> li = dm.GetModelList(" document_id = " + dpid);

            if (li.Count == 1)
            {
                Maticsoft.Model.documentFlowInfo d = li[0];

                string[] flow_map_ids = d.flow_ids.Split('|');
 
                //获取当前审批人审批节点号，transfer的undefine4记录的是该由哪个审批人审批的节点号，如果要修改自身的workFlow，则节点号就是undefine4
                int flow_node_num_now = Convert.ToInt32(d.flow_node_num_now);
                int flow_node_id_now = 0;

                flow_node_id_now = wm.GetFlow_node_id_now(person_id, d.document_id.Value, flow_node_num_now);

                DataSet ds_lastWorkFlowInfo = wm.GetLastWorkFlowInfo(d.document_id.Value.ToString(), flow_node_id_now.ToString(), flow_node_num_now);
                DataSet ds_nowWorkFlowInfo = wm.GetNowWorkFlowInfo(d.document_id.Value.ToString(), flow_node_id_now.ToString(), flow_node_num_now);

                if (ds_lastWorkFlowInfo != null)//有上一步流程信息
                {
                    if (ds_lastWorkFlowInfo.Tables[0].Rows.Count > 0)
                    {
                        DataRow r = ds_lastWorkFlowInfo.Tables[0].Rows[0];
                        DataRow r_now = ds_nowWorkFlowInfo.Tables[0].Rows[0];

                        if (wm.Update(2, reason, DateTime.Now, r_now["workFlow_id"].ToString()))//修改本身意见为否决,添加处理意见、处理时间
                        {
                            d.flow_node_num_now = d.flow_node_num_now - 1;

                            dm.Update(d);

                            Maticsoft.Model.workFlow w = new Maticsoft.Model.workFlow();

                            w.document_id = Convert.ToInt32(r["document_id"]);
                            w.flow_node_id = Convert.ToInt32(r["flow_node_id"]);
                            w.flow_node_num = Convert.ToInt32(r["flow_node_num"]);
                            w.person_id = Convert.ToInt32(r["person_id"]);
                            w.workFlow_direction = Convert.ToInt32(r["workFlow_direction"]);
                            w.workFlow_status = 0;
                            w.workFlow_time = DateTime.Now;
                            w.workFlow_reson = "";
                            w.workFlow_isWork = Convert.ToBoolean(r["workFlow_isWork"]);
                            w.workFlow_submitId = Convert.ToInt32(r["workFlow_submitId"]);

                            wm.Add(w);
                        }
                    }
                }
                else
                {
                    DataRow r_now = ds_nowWorkFlowInfo.Tables[0].Rows[0];

                    if (wm.Update(2, reason, DateTime.Now, r_now["workFlow_id"].ToString()))//修改本身意见为否决,添加处理意见、处理时间
                    {
                        //d.flow_node_num_now = d.flow_node_num_now - 1;
                        d.status = 0;
                        dm.Update(d);

                       
                        wm.Delete(Convert.ToInt32(r_now["workFlow_id"]));
                    }
                }
   
                
            }
        }

        //否决处理
        public static void DealDisagree(Maticsoft.Model.documentFlowInfo t, int flow_node_id)
        {
            Maticsoft.BLL.documentFlowInfo dmf = new Maticsoft.BLL.documentFlowInfo();
            Maticsoft.Model.flow_node f = new Maticsoft.BLL.flow_node().GetModel(flow_node_id);//获取flow_node
            int flow_node_num = Convert.ToInt32(f.flow_node_num);

            t.status = 3;//公文被否决，审批未通过
            t.flow_node_num_now = flow_node_num;//在哪一个节点未通过

            try
            {
                dmf.Update(t);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }


        /// <summary>
        /// 分发处理（手工添加下一节点信息)
        /// </summary>
        /// <param name="t"></param>
        /// <param name="flow_node_id"></param>
        /// <param name="person_id">要分发给谁</param>
        /// <param name="submitId">谁提交的这个分发</param>
        public static void DealWorkFlowSend(Maticsoft.Model.documentFlowInfo t, int flow_node_id, int person_id, int submitId)
        {
            Maticsoft.BLL.flow_node fm = new Maticsoft.BLL.flow_node();
            Maticsoft.BLL.documentFlowInfo dfm = new Maticsoft.BLL.documentFlowInfo();
            Maticsoft.BLL.workFlow wmm = new Maticsoft.BLL.workFlow();

            Maticsoft.Model.flow_node f = fm.GetModel(flow_node_id);//获取flow_node

            if (f.flow_node_isLast)//如果是尾节点
            {
                Maticsoft.BLL.documentFlowMapping dfmapm = new Maticsoft.BLL.documentFlowMapping();
                //获取流程图
                List<Maticsoft.Model.documentFlowMapping> li_map = dfmapm.GetModelList
                    (" documentFlowMapping_name='" + t.documentFlowMapping_name + "'  and flow_id= " + t.flow_id);

                if (li_map.Count == 1)
                {
                    Maticsoft.Model.documentFlowMapping dmap = li_map[0];

                    if (dmap.isLastFlow)//判断是否为最后一个流程
                    {
                        t.isDone = true;//更改状态为处理完结
                    }
                    else
                    {

                        //获取下一个流程信息
                        Maticsoft.Model.documentFlowMapping dmap_next = dfmapm.GetModelList
                            (" documentFlowMapping_name='" + t.documentFlowMapping_name +
                            "' and flow_order = " + (dmap.flow_order.Value + 1) + " ")[0];

                        t.flow_id = dmap_next.flow_id;//设置下一个流程ID
                        t.flow_name = dmap_next.flow_name;//设置下一个流程名称

                        Maticsoft.Model.flow_node f_next = fm.GetModelList(" flow_id=" + t.flow_id + " and flow_node_num = 1 ")[0];

                        t.flow_node_id_now = f_next.flow_node_id;//设置当前节点
                        t.flow_node_num_now = f_next.flow_node_num;//当前节点顺序号
                        t.status = 1;//审批中
                        t.isDone = false;//设置总流程没有完结

                        //加入下一节点审核信息
                        //手工获取下一节点（没有内联approver）
                        DataSet ds_workFlow = wmm.GetFlowNodeByFlow_idAndFlow_node_numSend(t.flow_id.Value, t.flow_node_num_now.Value);
                        for (int i = 0; i < ds_workFlow.Tables[0].Rows.Count; i++)
                        {
                            Maticsoft.Model.workFlow w = new Maticsoft.Model.workFlow();
                            w.document_id = t.document_id;
                            w.flow_node_id = Convert.ToInt32(ds_workFlow.Tables[0].Rows[i]["flow_node_id"].ToString());
                            w.flow_node_num = Convert.ToInt32(ds_workFlow.Tables[0].Rows[i]["flow_node_num"].ToString());
                            w.person_id = person_id;
                            w.workFlow_direction = 0;//代表此节点是正向，非反向（退签）
                            w.workFlow_reson = "";
                            w.workFlow_status = 0;//审批状态(0、未审批，1、同意，2、否决(退回首节点))
                            w.workFlow_time = DateTime.Now;
                            w.workFlow_isWork = f_next.flow_node_isAutoNext;//根据流程节点是否自动分发设置此流程节点是否处于激活状态
                            w.workFlow_submitId = submitId;

                            wmm.Add(w);
                        }

                    }

                    dfm.Update(t);
                }
            }
            else
            {
                int flow_node_num = Convert.ToInt32(f.flow_node_num) + 1;//获取下一节点的号码

                t.status = 1;//公文审批中
                t.flow_node_num_now = flow_node_num;//设置公文流程步骤+1，即下一节点的号码

                //手工获取下一节点（没有内联approver）
                DataSet ds = wmm.GetFlowNodeByFlow_idAndFlow_node_numSend(t.flow_id.Value, flow_node_num);

                //获取下一结点的所有信息
                Maticsoft.Model.flow_node f_next = fm.GetModelList(" flow_id=" + t.flow_id + " and flow_node_num = " + flow_node_num)[0];

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    Maticsoft.Model.workFlow w = new Maticsoft.Model.workFlow();
                    w.document_id = t.document_id;
                    w.flow_node_id = Convert.ToInt32(ds.Tables[0].Rows[i]["flow_node_id"].ToString());
                    w.flow_node_num = Convert.ToInt32(ds.Tables[0].Rows[i]["flow_node_num"].ToString());
                    w.person_id = person_id;
                    w.workFlow_direction = 0;//代表此节点是正向，非反向（退签）
                    w.workFlow_reson = "";
                    w.workFlow_status = 0;//审批状态(0、未审批，1、同意，2、否决(退回首节点))
                    w.workFlow_time = DateTime.Now;
                    w.workFlow_isWork = f_next.flow_node_isAutoNext;//根据流程节点是否自动分发设置此流程节点是否处于激活状态
                    w.workFlow_submitId = submitId;//提交人id

                    wmm.Add(w);
                }

                dfm.Update(t);//修改公文为处理中

            }

        }



        #region 出库审核流程相关

        //同意处理
        public static void DealAgree_OutSumbit(string dpid, int person_id, string reason)
        {
            Maticsoft.BLL.documentFlowInfo_outSubmit dm = new Maticsoft.BLL.documentFlowInfo_outSubmit();
            Maticsoft.BLL.workFlow_outSubmit wm = new Maticsoft.BLL.workFlow_outSubmit();
            List<Maticsoft.Model.documentFlowInfo_outSubmit> li = new Maticsoft.BLL.documentFlowInfo_outSubmit().GetModelList(" document_id = '" + dpid+"'");

            if (li.Count == 1)
            {
                Maticsoft.Model.documentFlowInfo_outSubmit d = li[0];

                //获取当前审批人审批节点号
                int flow_node_num_now = Convert.ToInt32(d.flow_node_num_now);
                int flow_node_id_now = 0;

                flow_node_id_now = wm.GetFlow_node_id_now(person_id, d.document_id, flow_node_num_now);

                DataSet ds_nowWorkFlowInfo = wm.GetNowWorkFlowInfo(d.document_id.ToString(), flow_node_id_now.ToString(), flow_node_num_now);

                DataRow r_now = ds_nowWorkFlowInfo.Tables[0].Rows[0];

                if (wm.Update(1, reason, DateTime.Now, r_now["workFlow_id"].ToString()))//修改本身意见为同意,添加处理意见、处理时间
                {
                    int flow_node_id = wm.GetFlow_node_id(person_id, d.document_id, Convert.ToInt32(d.flow_node_num_now), d.flow_id.Value);

                    string str = wm.Submit_judge(flow_node_id, person_id, d.document_id, Convert.ToInt32(d.flow_node_num_now));//得到审核状态

                    if (str == "同意")//1、如果是会签非尾节点同意，插入下一节点
                    {
                        lcs.BLL.Tools.DealWorkFlow_OutSubmit(d, flow_node_id, person_id);

                    }

                    if (str == "尾节点同意")//2、如果是会签尾节点同意
                    {
                        lcs.BLL.Tools.DealLast_OutSubmit(d, person_id);

                    }

                    if (str == "非会签")//3、如果是非会签非尾节点，插入下一结点
                    {
                        lcs.BLL.Tools.DealWorkFlow_OutSubmit(d, flow_node_id, person_id);
                    }

                    if (str == "尾节点非会签")
                    {
                        lcs.BLL.Tools.DealLast_OutSubmit(d, person_id);
                    }
                }
            }


        }

        //非尾节点的处理
        public static void DealWorkFlow_OutSubmit(Maticsoft.Model.documentFlowInfo_outSubmit t, int flow_node_id, int submitId)
        {
            Maticsoft.BLL.flow_node_outSubmit fm = new Maticsoft.BLL.flow_node_outSubmit();
            Maticsoft.Model.flow_node_outSubmit f = fm.GetModel(flow_node_id);//获取flow_node

            int flow_node_num = Convert.ToInt32(f.flow_node_num) + 1;//获取下一节点的号码

            t.status = 1;//公文审批中
            t.flow_node_num_now = flow_node_num;//设置公文流程步骤+1，即下一节点的号码

            try
            {
                new Maticsoft.BLL.documentFlowInfo_outSubmit().Update(t);//修改公文为处理中
            }
            catch (Exception ex)
            {

                throw ex;
            }

            //获取下一结点的所有信息
            Maticsoft.BLL.workFlow_outSubmit wmm = new Maticsoft.BLL.workFlow_outSubmit();
            DataSet ds = wmm.GetFlowNodeByFlow_idAndFlow_node_num(t.flow_id.Value, flow_node_num);
            Maticsoft.Model.flow_node_outSubmit f_next = fm.GetModelList(" flow_id=" + t.flow_id + " and flow_node_num = " + flow_node_num)[0];

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Maticsoft.Model.workFlow_outSubmit w = new Maticsoft.Model.workFlow_outSubmit();
                w.document_id = t.document_id;
                w.flow_node_id = Convert.ToInt32(ds.Tables[0].Rows[i]["flow_node_id"].ToString());
                w.flow_node_num = Convert.ToInt32(ds.Tables[0].Rows[i]["flow_node_num"].ToString());
                w.person_id = Convert.ToInt32(ds.Tables[0].Rows[i]["person_id"].ToString());
                w.workFlow_direction = 0;//代表此节点是正向，非反向（退签）
                w.workFlow_reson = "";
                w.workFlow_status = 0;//审批状态(0、未审批，1、同意，2、否决(退回首节点))
                w.workFlow_time = DateTime.Now;
                w.workFlow_isWork = f_next.flow_node_isAutoNext;//根据流程节点是否自动分发设置此流程节点是否处于激活状态
                w.workFLow_submitId = submitId;

                wmm.Add(w);
            }


        }


        //尾节点处理
        public static void DealLast_OutSubmit(Maticsoft.Model.documentFlowInfo_outSubmit t, int submitId)
        {
            //判断是否是最后一个流程，如果不是则插入下一流程id，如果是，则流程完毕
            Maticsoft.BLL.documentFlowInfo_outSubmit dfm = new Maticsoft.BLL.documentFlowInfo_outSubmit();
            Maticsoft.BLL.documentFlowMapping_outSubmit dfmapm = new Maticsoft.BLL.documentFlowMapping_outSubmit();
            //获取流程图
            List<Maticsoft.Model.documentFlowMapping_outSubmit> li_map = dfmapm.GetModelList
                (" documentFlowMapping_name='" + t.documentFlowMapping_name + "'  and flow_id= " + t.flow_id);

            if (li_map.Count == 1)
            {
                Maticsoft.Model.documentFlowMapping_outSubmit dmap = li_map[0];

                t.isFlowDone = true;//此流程结束

                if (dmap.isLastFlow)//判断是否为最后一个流程
                {
                    t.isDone = true;//更改状态为处理完结

                    //将状态更新至出库清单表中out_storageList
                    List<lcs.Model.out_storageList> li = new lcs.BLL.out_storageList().GetModelList(" out_storagelist_batchid='"+t.document_id+"' ");

                    lcs.BLL.out_storageList om = new BLL.out_storageList();

                    foreach (lcs.Model.out_storageList o in li)
                    {
                        o.check_status = "审核通过";
                        //o.check_reason = this.txt_reason.Text;
                        om.Update(o);
                    }

                }
                else
                {

                    //获取下一个流程信息
                    Maticsoft.Model.documentFlowMapping_outSubmit dmap_next = dfmapm.GetModelList
                        (" documentFlowMapping_name='" + t.documentFlowMapping_name +
                        "' and flow_order = " + (dmap.flow_order.Value + 1) + " ")[0];

                    t.flow_id = dmap_next.flow_id;//设置下一个流程ID
                    t.flow_name = dmap_next.flow_name;//设置下一个流程名称

                    Maticsoft.BLL.flow_node_outSubmit fm = new Maticsoft.BLL.flow_node_outSubmit();
                    Maticsoft.Model.flow_node_outSubmit f = fm.GetModelList(" flow_id=" + t.flow_id + " and flow_node_num = 1 ")[0];

                    t.flow_node_id_now = f.flow_node_id;//设置当前节点
                    t.flow_node_num_now = f.flow_node_num;//当前节点顺序号
                    t.status = 1;//审批中
                    t.isDone = false;//设置总流程没有完结

                    //加入下一节点审核信息
                    Maticsoft.BLL.workFlow_outSubmit wmm = new Maticsoft.BLL.workFlow_outSubmit();
                    DataSet ds_workFlow = wmm.GetWorkFlow(t.flow_id.Value, t.flow_node_num_now.Value);
                    for (int i = 0; i < ds_workFlow.Tables[0].Rows.Count; i++)
                    {
                        Maticsoft.Model.workFlow_outSubmit w = new Maticsoft.Model.workFlow_outSubmit();
                        w.document_id = t.document_id;
                        w.flow_node_id = Convert.ToInt32(ds_workFlow.Tables[0].Rows[i]["flow_node_id"].ToString());
                        w.flow_node_num = Convert.ToInt32(ds_workFlow.Tables[0].Rows[i]["flow_node_num"].ToString());
                        w.person_id = Convert.ToInt32(ds_workFlow.Tables[0].Rows[i]["person_id"].ToString());
                        w.workFlow_direction = 0;//代表此节点是正向，非反向（退签）
                        w.workFlow_reson = "";
                        w.workFlow_status = 0;//审批状态(0、未审批，1、同意，2、否决(退回首节点))
                        w.workFlow_time = DateTime.Now;
                        w.workFlow_isWork = f.flow_node_isAutoNext;//根据流程节点是否自动分发设置此流程节点是否处于激活状态
                        w.workFLow_submitId = submitId;

                        wmm.Add(w);
                    }

                }

                dfm.Update(t);

            }

        }



        /// <summary>
        /// 否决处理返回上一步
        /// </summary>
        /// <param name="dpid"></param>
        /// <param name="person_id"></param>
        /// <param name="reason"></param>
        public static void DealDisagreeLast_OutSubmit(string dpid, int person_id, string reason)
        {
            Maticsoft.BLL.documentFlowInfo_outSubmit dm = new Maticsoft.BLL.documentFlowInfo_outSubmit();
            Maticsoft.BLL.workFlow_outSubmit wm = new Maticsoft.BLL.workFlow_outSubmit();
            List<Maticsoft.Model.documentFlowInfo_outSubmit> li = dm.GetModelList(" document_id = '" + dpid+"'");

            if (li.Count == 1)
            {
                Maticsoft.Model.documentFlowInfo_outSubmit d = li[0];

                string[] flow_map_ids = d.flow_ids.Split('|');

                //获取当前审批人审批节点号，transfer的undefine4记录的是该由哪个审批人审批的节点号，如果要修改自身的workFlow，则节点号就是undefine4
                int flow_node_num_now = Convert.ToInt32(d.flow_node_num_now);
                int flow_node_id_now = 0;

                flow_node_id_now = wm.GetFlow_node_id_now(person_id, d.document_id, flow_node_num_now);

                DataSet ds_lastWorkFlowInfo = wm.GetLastWorkFlowInfo(d.document_id.ToString(), flow_node_id_now.ToString(), flow_node_num_now);
                DataSet ds_nowWorkFlowInfo = wm.GetNowWorkFlowInfo(d.document_id.ToString(), flow_node_id_now.ToString(), flow_node_num_now);

                if (ds_lastWorkFlowInfo != null)//有上一步流程信息
                {
                    if (ds_lastWorkFlowInfo.Tables[0].Rows.Count > 0)
                    {
                        DataRow r = ds_lastWorkFlowInfo.Tables[0].Rows[0];
                        DataRow r_now = ds_nowWorkFlowInfo.Tables[0].Rows[0];

                        if (wm.Update(2, reason, DateTime.Now, r_now["workFlow_id"].ToString()))//修改本身意见为否决,添加处理意见、处理时间
                        {
                            d.flow_node_num_now = d.flow_node_num_now - 1;

                            dm.Update(d);

                            Maticsoft.Model.workFlow_outSubmit w = new Maticsoft.Model.workFlow_outSubmit();

                            w.document_id = Convert.ToString(r["document_id"]);
                            w.flow_node_id = Convert.ToInt32(r["flow_node_id"]);
                            w.flow_node_num = Convert.ToInt32(r["flow_node_num"]);
                            w.person_id = Convert.ToInt32(r["person_id"]);
                            w.workFlow_direction = Convert.ToInt32(r["workFlow_direction"]);
                            w.workFlow_status = 0;
                            w.workFlow_time = DateTime.Now;
                            w.workFlow_reson = "";
                            w.workFlow_isWork = Convert.ToBoolean(r["workFlow_isWork"]);
                            w.workFLow_submitId = Convert.ToInt32(r["workFlow_submitId"]);

                            wm.Add(w);
                        }
                    }
                }
                else
                {
                    DataRow r_now = ds_nowWorkFlowInfo.Tables[0].Rows[0];

                    if (wm.Update(2, reason, DateTime.Now, r_now["workFlow_id"].ToString()))//修改本身意见为否决,添加处理意见、处理时间
                    {
                        //d.flow_node_num_now = d.flow_node_num_now - 1;
                        d.status = 0;
                        dm.Update(d);

                        //如果是第一步审核没通过则删除出库单
                        //lcs.BLL.out_storageList om = new out_storageList();

                        //List<lcs.Model.out_storageList> li_outlist = om.GetModelList(" out_storagelist_batchid='" + d.document_id + "' ");

                        //foreach (lcs.Model.out_storageList o in li_outlist)
                        //{
                        //    om.Delete(o.out_storageList_id);
                        //}

                        

                        //如果是第一步审核未通过，则修改outstoragelist状态为 “审核未通过”
                        lcs.BLL.out_storageList om = new out_storageList();

                        List<lcs.Model.out_storageList> li_outlist = om.GetModelList(" out_storagelist_batchid='" + d.document_id + "' ");

                        foreach (lcs.Model.out_storageList o in li_outlist)
                        {
                            o.check_status = "审核未通过";
                            o.check_reason = reason;

                            om.Update(o);
                        }

                        wm.Delete(Convert.ToInt32(r_now["workFlow_id"]));
                    }
                }


            }
        }

        #endregion

    }
}
